package com.futu.intelligence.controller;


import com.futu.intelligence.entity.Attraction;
import com.futu.intelligence.entity.Favorite;
import com.futu.intelligence.entity.TravelPlan;
import com.futu.intelligence.service.TravelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api")
@CrossOrigin(origins = "*") // 允许跨域请求
public class ApiController {

    @Autowired
    private TravelService travelService;

    /**
     * 生成旅游计划
     * POST /api/travel/generate
     */
    @PostMapping("/travel/generate")
    public Map<String, Object> generatePlan(@RequestBody Map<String, Object> request) {
        Map<String, Object> result = new HashMap<>();

        try {
            String city = (String) request.get("city");
            Integer days = (Integer) request.get("days");
            Long userId = (Long) request.get("userId");

            if (city == null || days == null || userId == null) {
                result.put("success", false);
                result.put("message", "参数不完整");
                return result;
            }

            TravelPlan plan = travelService.generatePlan(city, days, userId);

            if (plan != null) {
                result.put("success", true);
                result.put("data", plan);
                result.put("message", "旅游计划生成成功");
            } else {
                result.put("success", false);
                result.put("message", "生成旅游计划失败");
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "服务器错误: " + e.getMessage());
        }

        return result;
    }

    /**
     * 获取旅游计划详情
     * GET /api/travel/plan/{id}
     */
    @GetMapping("/travel/plan/{id}")
    public Map<String, Object> getPlanDetail(@PathVariable("id") Integer planId) {
        Map<String, Object> result = new HashMap<>();

        try {
            TravelPlan plan = travelService.getPlanDetail(planId);

            if (plan != null) {
                result.put("success", true);
                result.put("data", plan);
            } else {
                result.put("success", false);
                result.put("message", "旅游计划不存在");
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "服务器错误: " + e.getMessage());
        }

        return result;
    }

    /**
     * 获取用户收藏的旅游计划
     * GET /api/travel/favorites/{userId}
     */
    @GetMapping("/travel/favorites/{userId}")
    public Map<String, Object> getUserFavorites(@PathVariable("userId") Long userId) {
        Map<String, Object> result = new HashMap<>();

        try {
            List<Favorite> favorites = travelService.getUserFavorites(userId);
            result.put("success", true);
            result.put("data", favorites);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "服务器错误: " + e.getMessage());
        }

        return result;
    }

    /**
     * 收藏旅游计划
     * POST /api/travel/favorite
     */
    @PostMapping("/travel/favorite")
    public Map<String, Object> favoritePlan(@RequestBody Map<String, Object> request) {
        Map<String, Object> result = new HashMap<>();

        try {
            Long userId = (Long) request.get("userId");
            Integer planId = (Integer) request.get("planId");

            if (userId == null || planId == null) {
                result.put("success", false);
                result.put("message", "参数不完整");
                return result;
            }

            boolean success = travelService.favoritePlan(userId, planId);
            result.put("success", success);
            result.put("message", success ? "收藏成功" : "收藏失败");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "服务器错误: " + e.getMessage());
        }

        return result;
    }

    /**
     * 取消收藏
     * POST /api/travel/cancelFavorite
     */
    @PostMapping("/travel/cancelFavorite")
    public Map<String, Object> cancelFavorite(@RequestBody Map<String, Object> request) {
        Map<String, Object> result = new HashMap<>();

        try {
            Long userId = (Long) request.get("userId");
            Integer planId = (Integer) request.get("planId");

            if (userId == null || planId == null) {
                result.put("success", false);
                result.put("message", "参数不完整");
                return result;
            }

            boolean success = travelService.cancelFavorite(userId, planId);
            result.put("success", success);
            result.put("message", success ? "取消收藏成功" : "取消收藏失败");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "服务器错误: " + e.getMessage());
        }

        return result;
    }

    /**
     * 获取热门景点
     * GET /api/attractions/hot
     */
    @GetMapping("/attractions/hot")
    public Map<String, Object> getHotAttractions() {
        Map<String, Object> result = new HashMap<>();

        try {
            List<Attraction> attractions = travelService.getHotAttractions();
            result.put("success", true);
            result.put("data", attractions);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "服务器错误: " + e.getMessage());
        }

        return result;
    }

    /**
     * 健康检查接口
     * GET /api/health
     */
    @GetMapping("/health")
    public Map<String, Object> healthCheck() {
        Map<String, Object> result = new HashMap<>();
        result.put("success", true);
        result.put("message", "服务正常运行");
        result.put("timestamp", System.currentTimeMillis());
        return result;
    }
}
